import {
  apiAuditSendCode,
  apiAuditUpdatePassword,
  apiAuditUpdatePhone,
  apiUserAddFeedback,
  apiLogout,
  apiResetPassword
} from '../other/api'

import { regeneratorRuntime } from '../other/bundle'

const initState = {
  uiCodeCount: 60,
  auditSMSCode: {
    actionStatus: 'INIT',
    data: {}
  },
  phoneUpdateRes: {
    actionStatus: 'INIT',
    data: {}
  },
  passwordUpdateRes: {
    actionStatus: 'INIT',
    data: {}
  },
  feedbackRes: {
    actionStatus: 'INIT',
    data: {}
  }
}

export const getAuditSendCode = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    let intervalKey = null
    try {
      const apiRes = await apiAuditSendCode(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }

        intervalKey = setInterval(() => {
          const nextCount = global.store.getState().profileReducer.uiCodeCount - 1
          dispatch({
            type: 'SET_UI_SMS_CODE',
            data: nextCount > 0 ? nextCount : 60
          })
          if (nextCount <= 0) {
            clearInterval(intervalKey)
          }
        }, 1000)
      } else {
        throw new Error(apiRes)
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'GET_AUDIT_SMS_CODE' })
    return data;
  }
}

export const updatePhone = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiAuditUpdatePhone(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_UPDATE_PHONE_RES' })
    return data;
  }
}

export const updatePassword = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiAuditUpdatePassword(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_UPDATE_PASSWORD_RES' })
    return data;
  }
}

export const userAddFeedback = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiUserAddFeedback(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_ADD_FEEDBACK_RES' })
    return data;
  }
}

export const userLogout = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiLogout(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_LOGOUT_RES' })
    return data;
  }
}

export const resetPassword = params => {
  return async dispatch => {
    let data = {
      actionStatus: 'PENDING'
    }
    try {
      const apiRes = await apiResetPassword(params)
      if (apiRes.apiCode === 200 && apiRes.apiStatus === 'SUCCESS') {
        data = {
          actionStatus: 'SUCCESS',
          ...apiRes
        }
      } else {
        throw new Error('UNKNOW_ERROR ' + JSON.stringify(apiRes))
      }
    } catch (error) {
      console.error('捕获异常', error)
      data = {
        actionStatus: 'FAILURE',
        ...error
      }
    }
    await dispatch({ data, type: 'SET_RESET_PASSWORD_RES' })
    return data;
  }
}


export const reducer = (state = initState, action) => {
  switch (action.type) {
    case 'SET_UI_SMS_CODE':
      return {
        ...state,
        uiCodeCount: action.data
      }
      break;
    case 'GET_AUDIT_SMS_CODE':
      return {
        ...state,
        auditSMSCode: action.data
      }
      break;
    case 'SET_UPDATE_PHONE_RES':
      return {
        ...state,
        phoneUpdateRes: action.data
      }
      break;
    case 'SET_UPDATE_PASSWORD_RES':
      return {
        ...state,
        passwordUpdateRes: action.data
      }
      break;
    case 'SET_ADD_FEEDBACK_RES':
    return {
      ...state,
      feedbackRes: action.data
    }
    default:
      return state
      break;
  }
}

export default {
  reducer,
  getAuditSendCode,
  updatePhone,
  updatePassword,
}